/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.entity.report.TimeProduction;
import com.thienloc.omap.session.RequestQueueFacade;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class ProductionByTimeBean extends BaseBean implements Serializable {

    @EJB
    private RequestQueueFacade requestQueueFacade;
    private String monthStr = "";
    private String daysStr;
    private Boolean byMonth;
    private List<TimeProduction> productionMonths = new ArrayList<TimeProduction>();
    private List<TimeProduction> productionDays = new ArrayList<TimeProduction>();
    private final String SQL_DATE_PATTERN_MONTH = "MM/yyyy";
    private final String SQL_DATE_PATTERN_DAY = "dd/MM/yyyy";
    private final Logger logger = LoggerFactory.getLogger(ProductionByTimeBean.class);

    @PostConstruct
    public void init() {
        try {
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByMonth() {
        try {
            productionMonths = requestQueueFacade.countByTime(monthStr.split(","), SQL_DATE_PATTERN_MONTH);
            TimeProduction total = new TimeProduction();
            total.setDate("Tổng");
            for (TimeProduction timeProduction : productionMonths) {
                total.setP6071(timeProduction.getP6071() + total.getP6071());
                total.setP6171(timeProduction.getP6171() + total.getP6171());
                total.setP6271(timeProduction.getP6271() + total.getP6271());
                total.setP6371(timeProduction.getP6371() + total.getP6371());
                total.setP6471(timeProduction.getP6471() + total.getP6471());
                total.setP6571(timeProduction.getP6571() + total.getP6571());
                total.setP6671(timeProduction.getP6671() + total.getP6671());
                total.setP6771(timeProduction.getP6771() + total.getP6771());
            }
            productionMonths.add(total);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        for (int i = 0; i < 3; i++) {
            monthStr += new SimpleDateFormat(SQL_DATE_PATTERN_MONTH).format(cal.getTime());
            monthStr += ",";
            cal.add(Calendar.MONTH, -1);
        }

        monthStr.substring(0, monthStr.length() - 1);
        reportByMonth();
    }

    public void reportByDay() {
        try {
            productionDays = requestQueueFacade.countByTime(daysStr.split(","), SQL_DATE_PATTERN_DAY);
            TimeProduction total = new TimeProduction();
            total.setDate("Tổng");
            for (TimeProduction timeProduction : productionDays) {
                total.setP6071(timeProduction.getP6071() + total.getP6071());
                total.setP6171(timeProduction.getP6171() + total.getP6171());
                total.setP6271(timeProduction.getP6271() + total.getP6271());
                total.setP6371(timeProduction.getP6371() + total.getP6371());
                total.setP6471(timeProduction.getP6471() + total.getP6471());
                total.setP6571(timeProduction.getP6571() + total.getP6571());
                total.setP6671(timeProduction.getP6671() + total.getP6671());
                total.setP6771(timeProduction.getP6771() + total.getP6771());
            }
            productionDays.add(total);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByLast7Day() {
        Calendar cal = Calendar.getInstance();
        StringBuilder sb = new StringBuilder();
        SimpleDateFormat dateFormat = new SimpleDateFormat(SQL_DATE_PATTERN_DAY);
        for (int i = 0; i < 7; i++) {
            sb.append(dateFormat.format(cal.getTime()));
            sb.append(",");
            cal.add(Calendar.DATE, -1);
        }
        sb.deleteCharAt(sb.length() - 1);
        daysStr = sb.toString();
        reportByDay();
    }

    public Boolean getByMonth() {
        return byMonth;
    }

    public void setByMonth(Boolean byMonth) {
        this.byMonth = byMonth;
    }

    public List<TimeProduction> getProductionDays() {
        return productionDays;
    }

    public void setProductionDays(List<TimeProduction> productionDays) {
        this.productionDays = productionDays;
    }

    public List<TimeProduction> getProductionMonths() {
        return productionMonths;
    }

    public void setProductionMonths(List<TimeProduction> productionMonths) {
        this.productionMonths = productionMonths;
    }

    public String getDaysStr() {
        return daysStr;
    }

    public void setDaysStr(String daysStr) {
        this.daysStr = daysStr;
    }

    public String getMonthStr() {
        return monthStr;
    }

    public void setMonthStr(String monthStr) {
        this.monthStr = monthStr;
    }
}
